﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MTC.Common;
using MTC.Common.Enum;
using MTC.Web.Business.DTO;

namespace MTC.Web.Forms
{
    public partial class OwnerEdit : System.Web.UI.Page
    {
        private OwnerEntity _entity;
        Business.Owners OwnersBO = new Business.Owners();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                SetupEnvironment();
                //get the driver id if available
                if (Request.Params[Params.OWNERID] != "0")
                {
                    int ownerId = Convert.ToInt32(Request.Params[Params.OWNERID]);
                    _entity = OwnersBO.GetAOwner(ownerId);
                    LoadFields();
                }
            }

        }

        private void LoadFields()
        {
            if (_entity == null)
                return;
            hfOwnerId.Value = _entity.OwnerId.ToString();

            txtABN.Text = _entity.ABN;
            txtCompanyName.Text = _entity.CompanyName;
            txtFirstName.Text = _entity.FirstName;
            txtLastName.Text = _entity.LastName;
            txtMobile.Text = _entity.Mobile;
            txtPhone.Text = _entity.Phone;
            txtPostCode.Text = _entity.Postcode;
            txtState.Text = _entity.State;
            txtStreet1.Text = _entity.Street1;
            txtStreet2.Text = _entity.Street2;
            txtSuburb.Text = _entity.Suburb;
            txtTitle.Text = _entity.Title;
        }

        private bool SetFields()
        {
            OwnerEntity entity = new OwnerEntity();

            if (hfOwnerId.Value != "")
                entity.OwnerId = Convert.ToInt32(hfOwnerId.Value);

            entity.FirstName = txtFirstName.Text;
            entity.ABN = txtABN.Text;
            entity.CompanyName = txtCompanyName.Text;
            entity.LastName = txtLastName.Text;
            entity.Mobile = txtMobile.Text;
            entity.Phone = txtPhone.Text;
            entity.Postcode = txtPostCode.Text;
            entity.State = txtState.Text;
            entity.Street1 = txtStreet1.Text;
            entity.Street2 = txtStreet2.Text;
            entity.Suburb = txtSuburb.Text;
            entity.Title = txtTitle.Text;

            if (entity.OwnerId == 0)
            {
                OwnersBO.AddOwner(entity);
            }
            else
            {
                return OwnersBO.UpdateAOwner(entity);
            }
            return true;

        }

        private void SetupEnvironment()
        {
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            if (ValidateFields())
            {
                SetFields();
            }
        }

        private bool ValidateFields()
        {
            return true;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearFields();
            Response.Redirect(Common.Urls.OwnerListURL(), true);

        }

        private void ClearFields()
        {
            txtABN.Text = "";
            txtCompanyName.Text = "";
            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtMobile.Text = "";
            txtPhone.Text = "";
            txtPostCode.Text = "";
            txtState.Text = "";
            txtStreet1.Text = "";
            txtStreet2.Text = "";
            txtSuburb.Text = "";
            txtTitle.Text = "";
        }
    }
}
